<?php

namespace app\common\model\Product;

use think\Model;
use traits\model\SoftDelete;

class Product extends Model
{
    // 表名
    protected $name = 'product';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 软删除
    use SoftDelete;

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'flag_text',
        'status_text',
        'cover_cdn',
        'thumbs_cdn',
        'product_num',
        'rent_status_text'
    ];

    // 数据表
    protected $table = 'pre_product';

    public function getFlagList()
    {
        return ['1' => __('新品'), '2' => __('热销'), '3' => __('推荐')];
    }


    public function getFlagTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['flag']) ? $data['flag'] : '');
        $list = $this->getFlagList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStatusList()
    {
        return ['0' => __('下架'), '1' => __('上架')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getRentStatusList()
    {
        return ['1' => '普通商品', '2' => '仅租商品', '3' => '既购买也能租赁的商品'];
    }

    public function getRentStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rentstatus']) ? $data['rentstatus'] : '');
        $list = $this->getRentStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    // 时间修改器
    public function setCreatetimeAttr($value, $data)
    {
        return strtotime($value);
    }

    // 分类关联查询
    public function type()
    {
        return $this->belongsTo('app\common\model\Product\Type', 'typeid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // 单位关联查询
    public function unit()
    {
        return $this->belongsTo('app\common\model\Product\Unit', 'unitid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    // 商品封面图-20230504更新
    public function getCoverCdnAttr($value, $data)
    {
        // 20230504更新
        $thumbsArr = explode(',', $data['thumbs']);

        // 20230504更新
        $thumbsArr = array_filter($thumbsArr);

        $cdn = config('site.url');

        $thumbs = empty($data['thumbs']) ? [] : trim($data['thumbs']);

        if (empty($thumbs)) {
            return $cdn . '/assets/home/images/cbd.jpg';
        }

        //字符串替换
        $thumbs = str_replace("/uploads/", $cdn . "/uploads/", $thumbs);

        //在转换为数组
        $list = explode(',', $thumbs);

        if (count($list) > 0) {
            return $list[0];
        }

        // 20230504更新
        return $cdn . $thumbsArr[0];
    }

    public function getThumbsCdnAttr($value, $data)
    {
        $cdn = config('site.url');

        $thum = empty($data['thumbs']) ? [] : trim($data['thumbs']);

        if (empty($thum)) {
            return $cdn . '/assets/home/images/cbd.jpg';
        }

        // 用字符串替换函数 把域名追加
        $thumbs = str_replace('/uploads', $cdn . '/uploads', $data['thumbs']);

        //再转换为数组
        $thumbsArr = explode(',', $thumbs);

        $thumbsArr = array_filter($thumbsArr);

        // if (count($thumbsArr) > 0) {
        //     return $thumbsArr;
        // }

        return $thumbsArr;
    }

    public function getProductNumAttr($value, $data)
    {
        $Count = model('Order.Product')->where(['proid' => $data['id']])->count('pronum');

        return $Count;
    }
}
